/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.item.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.modules.item.entity.ItemEntity;
import org.springblade.modules.item.entity.ItemImgEntity;
import org.springblade.modules.item.service.IItemService;
import org.springblade.modules.item.vo.ItemImgVO;
import org.springblade.modules.item.mapper.ItemImgMapper;
import org.springblade.modules.item.service.IItemImgService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.transaction.annotation.Transactional;

/**
 * 教具图片 服务实现类
 *
 * @author
 * @since 2023-03-22
 */
@Service
public class ItemImgServiceImpl extends BaseServiceImpl<ItemImgMapper, ItemImgEntity> implements IItemImgService {

	@Override
	public IPage<ItemImgVO> selectItemImgPage(IPage<ItemImgVO> page, ItemImgVO itemImg) {
		return page.setRecords(baseMapper.selectItemImgPage(page, itemImg));
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean updateIsMain(Long id, String itemId,Integer isMain) {
		ItemImgEntity imgEntity=new ItemImgEntity();
		imgEntity.setId(id);
		imgEntity.setIsMain(isMain);
		if (isMain==1){
			//把原本主图的isMain设为0
			ItemImgEntity mainImg = baseMapper.selectOne(Wrappers.<ItemImgEntity>query().lambda().eq(ItemImgEntity::getItemId, itemId).eq(ItemImgEntity::getIsMain, isMain));
			if (null!=mainImg){
				mainImg.setIsMain(0);
				baseMapper.updateById(mainImg);
			}
		}

		return baseMapper.updateById(imgEntity)>0;
	}


}
